export  function useWebsocket (handleMessage,username) {
    const ws = new WebSocket(`ws://localhost:8080/chat?username=${username}/`);


    const init = () =>{
        bindEvent();

    }

    function bindEvent(){
        ws.addEventListener('open',handleOpen,false);
        ws.addEventListener('close',handleClose,false);
        ws.addEventListener('error',handleError,false);
        ws.addEventListener('message',handleMessage,false);
    }

    function handleOpen(e) {
        console.log('Websocket open',e);
        
    }

    function handleClose(e){
        console.log('WebSocket Close',e);
    }

    function handleError(e){
        console.log('Websocket error',e);
    }


    init();

    return ws;

}